﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangLego.Model;
using CuaHangLego.DataAccess;
using CuaHangLego.Bussiness;
using System.Threading;
using CuaHangLego.Data;

namespace CuaHangLego.UI
{
    public partial class QLNhaCungCapUI : UserControl
    {
        public NhaCungCapBSS bss;
        private int idx = -1;

        public QLNhaCungCapUI()
        {
            InitializeComponent();
            this.bss = new NhaCungCapBSS();
            tblDSNCC.AutoGenerateColumns = false;
        }

        private void QLNhaCungCapUI_Load(object sender, EventArgs e)
        {
            this.tblDSNCC.DataSource = NhaCungCapDA.Instance.GetAll();
        }

        private void tblDSNCC_SelectionChanged(object sender, EventArgs e)
        {
            if (tblDSNCC.SelectedRows.Count > 0)
            {
                if (tblDSNCC.SelectedRows[0].Index != idx)
                {
                    idx = tblDSNCC.SelectedRows[0].Index;
                    if (!backgroundWorker1.IsBusy)
                    {
                        backgroundWorker1.RunWorkerAsync();
                    }
                }
            }
        }

        private void buttonTimNCC_Click(object sender, EventArgs e)
        {
            this.tblDSNCC.DataSource = bss.GetNCCBy(txtKeyNCC.Text);
        }

        private void buttonXemTatCaNCC_Click(object sender, EventArgs e)
        {
            this.tblDSNCC.DataSource = bss.GetNCCAll();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            bss.CreateData((tblDSNCC.SelectedRows[0].DataBoundItem as NhaCungCap).MaSo);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            txtTongHD.Text = bss.SoHD.ToString();
            txtDaMua.Text = bss.TongSP.ToString();
            txtTongTien.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", bss.TongTien);
        }
    }
}
